//
//  FindViewController.m
//  TasteEnjoy
//
//  Created by XuRui on 16/4/28.
//  Copyright © 2016年 XuRui. All rights reserved.
//

//
#warning 3.Find  collection分区

#import "FindViewController.h"

//10
@interface FindViewController ()<UICollectionViewDelegate, UICollectionViewDataSource>

@property (nonatomic, retain) BaseCollectionView *collectionView;  /**< collection */
@property (nonatomic, retain) FindLayout *findLayout;

@property (nonatomic, retain) NSMutableArray *arrHeaderName; /**< 分区名 */
@property (nonatomic, retain) NSMutableArray *arrFind; /**< 整个 数组里5个字典 */
@property (nonatomic, retain) NSMutableArray *arrPop;
@property (nonatomic, retain) NSMutableArray *arrTiansheng;
@property (nonatomic, retain) NSMutableArray *arrBaiRi;
@property (nonatomic, retain) NSMutableArray *arrFashion;
@property (nonatomic, retain) NSMutableArray *arrChaoMeng;
@property (nonatomic, retain) NSMutableArray *arrDetail;
@property (nonatomic, retain) NSArray *arrFirstSectionPop;
@end

@implementation FindViewController

#pragma mark - ------------ 内存管理 ---------------
- (void)dealloc
{
    [_collectionView release];
    [_findLayout release];
    
    [_arrHeaderName release];
    [_arrFind release];
    [_arrPop release];
    [_arrTiansheng release];
    [_arrBaiRi release];
    [_arrFashion release];
    [_arrChaoMeng release];
    [_arrDetail release];
    [_arrFirstSectionPop release];
    _collectionView.delegate = nil;
    _collectionView.dataSource = nil;
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title = @"图库";
    
  
    //搜索
   self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc]initWithImage:[[UIImage imageNamed:@"sousuo"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(sousuo:)]autorelease];
    
    [self dataHandler];
    [self createCollectionView];
    [self refresh];
    
}
#pragma mark - ------------ 点击搜索 ---------------
- (void)sousuo:(UIBarButtonItem *)item
{
    
    SearchViewController *search = [[SearchViewController alloc]init];
    
    [self.navigationController pushViewController:search animated:YES];
    [search release];
}

#pragma mark - ------------ 数据处理 ---------------
- (void)dataHandler
{

    [XrDataParse GETWithURL:@"http://203.80.144.213/napi/index/groups/?app_code=gandalf&platform_version=9.3.1&app_version=6.0.6%20rv%3A156728&screen_width=320&__domain=www.duitang.com&locale=zh_CN&screen_height=568&device_platform=iPhone5%2C2&platform_name=iPhone%20OS&device_name=iPhone%205" par:nil success:^(id responseObject) {
        
        NSArray *arr = responseObject[@"data"];
        
        //整个 放5个字典 分区数
        self.arrFind = [FindModel modelHandleWithArrar:arr];
       
        //流行趋势
        self.arrFirstSectionPop = arr[0][@"group_items"];
        self.arrPop  = [FindModel modelHandleWithArrar:_arrFirstSectionPop];
        
        //天生购物狂
        NSArray *arr1 = arr[1][@"group_items"];
        self.arrTiansheng  = [FindModel modelHandleWithArrar:arr1];
        
       //白日梦想家
        NSArray *arr2 = arr[2][@"group_items"];
        self.arrBaiRi  = [FindModel modelHandleWithArrar:arr2];

        //时尚女魔头
        NSArray *arr3 = arr[3][@"group_items"];
        self.arrFashion  = [FindModel modelHandleWithArrar:arr3];

        //超萌战队
        NSArray *arr4 = arr[4][@"group_items"];
        self.arrChaoMeng  = [FindModel modelHandleWithArrar:arr4];
        
        
        //分组名 出掉第一个字典
        self.arrHeaderName = [NSMutableArray array];
        for (NSDictionary *dic in arr) {
            
            [self.arrHeaderName addObject:dic[@"group_name"]];
        }
        if (_arrHeaderName.count != 0) {
            
            [_arrHeaderName removeObjectAtIndex:0];
        }
        
       [_collectionView reloadData];


    } filed:^(NSError *error) {

    }];

}

#pragma mark - ------------ 下拉刷新 ---------------

- (void)refresh
{
    //刷新
    self.collectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
        [self dataHandler];
        [_collectionView.mj_header endRefreshing];
    }];
    
}


#pragma mark - ------------ 创建collectionView ---------------
- (void)createCollectionView
{
    
    
    self.findLayout = [[FindLayout alloc]init];
  //  _findLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    
    self.collectionView = [[BaseCollectionView alloc]initWithFrame:[UIScreen mainScreen].bounds collectionViewLayout:_findLayout];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    _collectionView.showsVerticalScrollIndicator = YES;
    [self.view addSubview:_collectionView];
    
    //注册cell
    [_collectionView registerClass:[FindCollectionViewCell class] forCellWithReuseIdentifier:@"find"];
    //正常头部 流行趋势
    [_collectionView registerClass:[HeaderCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];
    //只有分区View 没有item
     [_collectionView registerClass:[ViewFirstSection class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"first"];
    
    [_findLayout release];
    [_collectionView release];
    
}


#pragma mark - ------------ cell赋值 根据不同分区 传不同数组 ---------------
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{

    FindCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"find" forIndexPath:indexPath];
    
    switch (indexPath.section) {
           
        case 2:
        {

            FindModel *m = _arrTiansheng[indexPath.item];
            
            cell.model = m;
            break;
        }
        case 3:
        {

           FindModel *m = _arrBaiRi[indexPath.item];
            
            cell.model = m;
         
            break;
        }
        case 4:
        {
        
            FindModel *m = _arrFashion[indexPath.item];
            cell.model = m;
         
            break;
        }
     
        default:{
            

            FindModel *m = _arrChaoMeng[indexPath.item];
            cell.model = m;

         
            break;
        }
        
    }
    
    return cell;
}

#pragma mark - ------------ 点击事件 ---------------
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{

    
    FindDetailViewController *detail = [[FindDetailViewController alloc]init];
    
    switch (indexPath.section) {
            
        case 2:
        {
            
            FindModel *m = _arrTiansheng[indexPath.item];
            detail.strDetail = m.target;
            detail.naviTitle = m.name;
            
            break;
        }
        case 3:
        {
            
            FindModel *m = _arrBaiRi[indexPath.item];
   
             detail.strDetail = m.target;
             detail.naviTitle = m.name;

            break;
        }
        case 4:
        {
            
            FindModel *m = _arrFashion[indexPath.item];
 
             detail.strDetail = m.target;
             detail.naviTitle = m.name;

            break;
        }
            
        default:
        {
            FindModel *m = _arrChaoMeng[indexPath.item];
            detail.strDetail = m.target;
            detail.naviTitle = m.name;

            break;
        }
    }
    
    
    
    [self.navigationController pushViewController:detail animated:YES];
    [detail release];
    
}

#pragma mark - ------------ 返回分区数5 + 1 ---------------
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    
    return _arrFind.count  + 1;

}


//共6 个分区
//没有 item 0:流行趋势 当1的区头 1:横向滑动的collecton
//剩余4个 正常的

#pragma mark - ------------ 按分区返回item个数 ---------------
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    
  /** 第0,1个分区 没有item 只要区头  */
    if (section == 0 ) {
        
      
       return 0;
   
    }else if (section == 1){
    
    
        return 0;
    }else {
        
        FindModel *f = _arrFind[section - 1];
   
        return f.group_items.count;
        
    }
    
    return 0;
}


#pragma mark - ------------ collection头部 ---------------
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    //第0分区头 作为第二个分区view的头部
  //  NSLog(@"item:%ld", indexPath.section);
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
        
        if (indexPath.section == 0) {
            
            HeaderCollectionReusableView *header = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"header" forIndexPath:indexPath];
            
            header.labelHeader.text = @"流行趋势";
            /** 夜间 */
            header.labelHeader.dk_textColorPicker = DKColorWithColors([UIColor blackColor], [UIColor grayColor]);
            header.dk_backgroundColorPicker = DKColorWithColors(GrayLight,[UIColor lightGrayColor]);
            return header;
            
            
        }else if (indexPath.section == 1){
            
            
            ViewFirstSection *header = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"first" forIndexPath:indexPath];
          
            header.arrPop = _arrPop;
  /** block传值 */
      
            [header clickFirstSectionView:^(NSIndexPath *indexPath) {
                
                FindModel *m = _arrPop[indexPath.item];
                
                NSDictionary *dic = _arrFirstSectionPop[indexPath.item];
                
                NSString *strTemp = [dic[@"target"] substringWithRange:NSMakeRange(40, 33)];
                
                FirstSectionViewController *detail = [[FirstSectionViewController alloc]init];
                
                detail.strID = strTemp;
                detail.strTitle = m.name;
                [self.navigationController pushViewController:detail animated:YES];
                [detail release];

                
            }];
        
        
            
            return header;
            
        }else{
            
            HeaderCollectionReusableView *header = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"header" forIndexPath:indexPath];
            
            header.labelHeader.text = _arrHeaderName[indexPath.section - 2];
            /** 夜间 */
            header.labelHeader.dk_textColorPicker = DKColorWithColors([UIColor blackColor], [UIColor grayColor]);
            header.dk_backgroundColorPicker = DKColorWithColors(GrayLight,[UIColor lightGrayColor]);
            return header;
            
        }
        
    }
    return nil;


}


#pragma mark - ------------ 返回自定义 header高度 ---------------

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
   
    if (section == 1) {
        
        return CGSizeMake(SCREEN_WIDTH, 140);
        
    }else{
       
        return CGSizeMake(SCREEN_WIDTH, 30);
    }
}


#pragma mark - ------------ 返回item大小 ---------------

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(nonnull NSIndexPath *)indexPath
{

//0,1 无item
    if (indexPath.section == 0 && indexPath.section == 1) {
        return  CGSizeZero;
    }else {
        
        return CGSizeMake(SCREEN_WIDTH / 2, 90);
    }
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
